package com.suarte.webapp.action;

import com.suarte.core.Company;
import java.io.Serializable;
import org.appfuse.service.GenericManager;

/**
 * @date   Dec 21, 2010
 * @author Ggutierrez
 */
public class CompanyForm extends BasePage implements Serializable {

    private GenericManager<Company, Long> companyManager;
    private Company company = new Company();
    private Float balance = 0f;
    private Float balanceLocal = 0f;
    private Long id;

    public void setCompanyManager(GenericManager<Company, Long> companyManager) {
        this.companyManager = companyManager;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        companyManager.remove(company.getId());
        addMessage("company.deleted");

        return "list";
    }

    public String edit() {
        if (id != null) {
            company = companyManager.get(id);
            balance = company.getBalance();
            balanceLocal = company.getBalanceLocal();
        } else {
            company = new Company();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (company.getId() == null);
        company.setBalance(balance);
        company.setBalanceLocal(balanceLocal);

        companyManager.save(company);

        String key = (isNew) ? "company.added" : "company.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }

    public Float getBalance() {
        return balance;
    }

    public void setBalance(Float balance) {
        this.balance = balance;
    }

    public Float getBalanceLocal() {
        return balanceLocal;
    }

    public void setBalanceLocal(Float balanceLocal) {
        this.balanceLocal = balanceLocal;
    }
}
